import * as services from 'services/report';
import { Toast } from 'antd-mobile';

export default {

  namespace: 'report',

  state: {
    reports: [],
    report: {},
    programs: [],
    member: {},
    comment: "",
    PatientValue: "",
    isDisabled: 'block',   
    Disabled: 'block',   
    reportName: "",// 可以通过这个动态的改变相对应的值
  },

  reducers: {// 处理数据
    save(state, action) {
      return { ...state, ...action.payload };
    },
  },

  effects: {// 接收数据
    *fetchPrePreReportDetail ({ payload: { type, uid }}, { call, put }){
       const detail = yield call(services.queryPrePreReportDetail, type, uid);
       if(detail.code == 200){
          yield put({ 
            type: 'save', 
            payload: {
              programs:{
                Programe: detail.data.result.programe,
                type: type,
                recType: detail.data.result.recType
              },
              report:{
                 report1: detail.data.result.report1,
                 report2: detail.data.result.report2,
                 report3: detail.data.result.report3,
                 vp: detail.data.vp,
              },
              comment: detail.data.comment,
              PatientValue:{
                 patientValue: detail.data.patientValue,
              },
           }});
       }else{
          Toast.fail(detail.message, 2);
       }
    },

    *fetchReportDetail({ payload: { type, uid, evauid, familyId} }, { call, put }) {
      let detail;
      if (type == "tongue" || type == "pulse" || type == "face" || type == "alpha") {
          familyId  = JSON.parse(localStorage.getItem("auth"+sessionStorage.getItem("hid"))).patientId;
          detail = yield call(services.queryReportDetail, type, evauid, familyId);
          if(detail.code == 200){
              let result = detail.data.result;
              let vp = detail.data.vp;
              yield put({
                type: 'save',
                payload: {
                  report: {
                     ...result,
                     vp
                  }

                }});
          }else{
              Toast.fail(detail.message, 2);
          }
      }else{
          familyId  = JSON.parse(localStorage.getItem("auth"+sessionStorage.getItem("hid"))).patientId;
          detail = yield call(services.queryReportDetail, type, evauid, familyId);
          if(detail.code == 200){
            yield put({ 
              type: 'save', 
              payload: { 
                programs:{
                  programeInt:detail.data.programeInt, 
                  programeRec:detail.data.programeRec, 
                  programeIntHtml:detail.data.programeIntHtml, 
                  programeRecHtml:detail.data.programeRecHtml, 
                  Programe:detail.data.programe,
                  IntDetail: detail.data.intDetail,
                  type: type, recType: detail.data.recType,
                  RecDetail:detail.data.recDetail,

                }, 
                comment:detail.data.comment, 

                PatientValue:{
                  patientValue: detail.data.patientValue,
                  patientValue1:detail.data.patientValue1, 
                  patientValue2:detail.data.patientValue2,
                  patientValue3:detail.data.patientValue3,
                },
                report:{
                  report1:detail.data.report1,
                  report2:detail.data.report2,
                  report3:detail.data.report3,
                  report4:detail.data.report4,
                  report5:detail.data.report5,
                  vp:detail.data.vp,
                  isTrust: detail.data.isTrust
                },

            }});
          }else{
             Toast.fail(detail.message, 2);
          }
      }
    }
  },

  subscriptions: {//监听数据
    setup({ dispatch, history }) {
      return history.listen(({ location, pathname, query, ...rest }) => {
        const sheetType = {
          1: 'old',
          2: 'nine',
          3: 'phy',
          4: 'pre',
          5: 'hyp',
          6: 'chd',
          11: 'dia',
          12: 'tcm',
          13: 'partum',
          14: 'prepre',
          16: 'normal',
          17: 'phy2',
        };
        if (/report$/.test(pathname)) {
          services.queryReports().then(reports =>{
            dispatch({ type: 'save', payload: { reports: reports.data || [] } })
         })
        }

        if (/report\/+/.test(pathname)) {
          const payload = { source: 1 };

          // source: 1(微信), 2(APP)
          // APP端会额外传 doctorid
          if (query.s && query.s === 'app') {
            payload.source = 2;
          }


          if (payload.source === 2) {
            payload.doctorid = query.doctorid;
            payload.uid = pathname.split('/').slice(-1)[0];

          } else {
            const { state: { report } } = rest;
            const {state} = rest;
            payload.uid = report.uid;
            payload.evauid = report.evaUid;
          }

          let eva;
          if (query.eva) {
            eva = query.eva;
          }

          if (rest.state && rest.state.report && rest.state.report.eva) {
            eva = rest.state.report.eva;
          }

          switch (parseInt(eva, 10)) {
            case 1:
              payload.type = 'old';
              break;
            case 2:
              payload.type = 'nine';
              break;
            case 3:
              payload.type = 'phy';
              break;
            case 4:
              payload.type = 'pre';
              break;
            case 5:
              payload.type = 'hyp';
              break;
            case 6:
              payload.type = 'chd';
              break;
            case 7:
              payload.type = 'alpha';
              break;
            case 8:
              payload.type = 'pulse';
              break;
            case 9:
              payload.type = 'tongue';
              break;
            case 10:
              payload.type = 'face';
              break;
            case 11:
              payload.type = 'dia';
              break;
            case 12:
              payload.type = 'tcm';
              break;
            case 13:
              payload.type = 'partum';
              break;
             case 14:
              payload.type = 'prepre';
              break;
             case 16:
              payload.type = 'normal';
              break; 
              case 17:
              payload.type = 'phy2';
              break; 
            default:
              // ...
          }
          if(rest.state.title === "备孕女性健康状态测评" || rest.state.title === "常态女性健康状态测评"){
              dispatch({ type: 'fetchPrePreReportDetail', payload:{
                 type: sheetType[rest.state.report.eva],
                 uid: rest.state.report.evaUid,
              } });
          }else{
              dispatch({ type: 'fetchReportDetail', payload });
          }
        }
      });
    },
  },
}
